package com.nbgroup.service;

import com.nbgroup.dao.LiteratureRepositoty;
import com.nbgroup.entity.Exhibition;
import com.nbgroup.entity.Literature;
import com.nbgroup.entity.LiteratureChild;
import com.nbgroup.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LiteratureService {

    @Autowired
    private LiteratureRepositoty literatureRepositoty;
    @Autowired
    private QueryUtil queryUtil;


    public List<Literature> findLiteratureByExhibtionId(Integer exhibitionId){
        System.out.println(exhibitionId);
        return  this.literatureRepositoty.findLiteratureByExhibtionId(exhibitionId);
    }

    public Literature findLiteratureById(Integer literatureId){
        return  this.literatureRepositoty.findOne(literatureId);
    }

    public List<Literature> findLiteratureListByExhibtionId(Integer exhibitionId){
        System.out.println(exhibitionId);
        return  this.literatureRepositoty.findLiteratureListByExhibtionId(exhibitionId);
    }

    /**
     * 查询 指定展厅 的展位
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public List<Literature> findAllForLiterature(Integer exhibtionId){
        String sql = "SELECT * FROM `literature` WHERE exhibtion_id = '"+exhibtionId+"' AND sts = '1'";
        List<Literature> literatureList = queryUtil.query(sql, Literature.class);
        return literatureList;
    }

    /**
     * 展位保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int saveLiterature(Literature literature){
        try{
            literature.setSts("1");
            literature.setLiteratureCount(0);
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literature.setLiteratureTime(dateStr);
            literatureRepositoty.save(literature);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateLiterature(Literature literature){
        try{
            literature.setSts("1");
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literature.setLiteratureTime(dateStr);
            literatureRepositoty.saveAndFlush(literature);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位阅读数修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateLiteratureForCount(Integer literatureId){
        try{
            Literature literature = this.findOneForLiterature(literatureId);
            literature.setLiteratureCount(literature.getLiteratureCount()+1);
            literatureRepositoty.saveAndFlush(literature);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展位findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public Literature findOneForLiterature(Integer literatureId){
        String sql = "SELECT * FROM `literature` WHERE literature_id = '"+literatureId+"' AND sts = '1'";
        List<Literature> literatureList = queryUtil.query(sql, Literature.class);
        if(literatureList != null){
            return literatureList.get(0);
        }else{
            return null;
        }
    }

    /**
     * 展位删除--逻辑
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int deleteLiterature(Integer literatureId){
        try{
            Literature literature = this.findOneForLiterature(literatureId);
            literature.setSts("0");
            String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            literature.setLiteratureTime(dateStr);
            literatureRepositoty.saveAndFlush(literature);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }
}
